package logMonitor.app.callback;

import com.google.gson.Gson;
import logMonitor.dao.LogMonitorDao;
import logMonitor.domain.Message;
import logMonitor.domain.Record;
import logMonitor.domain.Rule;
import logMonitor.utils.MyShardedJedisPool;
import redis.clients.jedis.ShardedJedis;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Describe: 计算每分钟的增量信息
 * Author:   maoxiangyi
 * Domain:   www.itcast.cn
 * Data:     2015/11/17.
 */
public class OneMinuteCallBack implements Runnable {
    public static  ShardedJedis jedis = MyShardedJedisPool.getShardedJedisPool().getResource();
    @Override
    public void run() {
        //从规则表读取当前的ruleList
        List<Rule> ruleListAlive = new LogMonitorDao().loadRuleAlive();
        List<Rule> ruleListDead = new LogMonitorDao().loadRuleDead();
        List<Record> recordList;
        if(ruleListAlive!=null){
            for(Rule rule : ruleListAlive) {
                recordList = getRecordListFromRedis(rule);
                try {
                    new LogMonitorDao().saveRecord(recordList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if(ruleListDead!=null){
            for(Rule rule : ruleListDead) {
                try {
                    new LogMonitorDao().deleteRecordByRuleId(new Integer(rule.getId()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public List<Record> getRecordListFromRedis(Rule rule){
        List<Record> recordList = new ArrayList<Record>();
        Record record;
        for (String name : jedis.zrevrange("ruleid:"+rule.getId(), 0, 4)) {
            String []str = name.split(":");//获取源ip str[0],目的ip str[1]
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            record = new Record(rule.getId(),str[0],str[1],date,jedis.zscore("ruleid:"+rule.getId(),name));
            recordList.add(record);
        }
        return recordList;
    }



//    public void run() {
//        //从数据库规则表得到ip
//        String ip ="127.0.0.1";
//        List<Message> messageList = new ArrayList<Message>();
//        messageList = getMessageListFromRedis(ip);
////        for(Message message :messageList){
////            System.out.println(message.getDst_ip() +"的开始放假还是");
////        }
//        //存储到mysql
//        try{
//            new LogMonitorDao().saveMessage(messageList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        //将redis对应的大key为规则表ip的list删除
//        jedis.del(ip);
//    }
    //已经废弃。根据ip（大key）从redis,list中得到messageList
    public List<Message> getMessageListFromRedis(String ip){
        List<Message> messageList = new ArrayList<Message>();
        for (String string : jedis.lrange(ip, 0, -1)) {
            Message message = (Message) new Gson().fromJson(string, Message.class);
            messageList.add(message);
        }
        return messageList;
    }
}
